import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue')
  },
  {
    path: '/home',
    component: () => import('../views/Home.vue'),
    redirect: '/'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue')
  },
  {
    path: '/detailcourse',
    name: 'Detailcourse',
    component: () => import('../views/Detailcourse.vue'),
    children: [{
        path: 'overview',
        name: 'overview',
        component: () => import('../components/ke-overview')
      },
      {
        path: 'catalogue',
        name: 'catalogue',
        component: () => import('../components/ke-catalogue')
      },
      {
        path: 'comment',
        name: 'comment',
        component: () => import('../components/ke-comment')
      }
    ]
  },
  {
    path: '/mine',
    name: 'mine',
    component: () => import('../views/Mine.vue'),
    children: [{
        path: 'timetable',
        name: 'timetable',
        component: () => import('../components/ke-time-table')
      },
      {
        path: 'order',
        name: 'order',
        component: () => import('../components/ke-order')
      },
      {
        path: 'collect',
        name: 'collect',
        component: () => import('../components/ke-collect')
      },
      {
        path: 'userinfo',
        name: 'userinfo',
        component: () => import('../components/ke-user-info')
      },
      {
        path: 'applycourse',
        name: 'applycourse',
        component: () => import('../components/ke-apply-course')
      },
      {
        path: 'mineUpload',
        name: 'mineUpload',
        component: () => import('../components/ke-mine-upload')
      }
    ]
  },
  {
    path: '/opencourse',
    name: 'opencourse',
    component: () => import('../views/Opencourse.vue'),
    children: [{
        path: 'overview',
        name: 'overview',
        component: () => import('../components/ke-opencourse-overview')
      },
      {
        path: 'catalogue',
        name: 'catalogue',
        component: () => import('../components/ke-opencourse-catalogue')
      },
      {
        path: 'comment',
        name: 'comment',
        component: () => import('../components/ke-opencourse-comment')
      }
    ]
  },
  {
    path: '/course',
    name: 'course',
    component: () => import('../views/Course.vue'),
    children: [{
        path: 'overview',
        name: 'overview',
        component: () => import('../components/ke-course-overview')
      },
      {
        path: 'catalogue',
        name: 'catalogue',
        component: () => import('../components/ke-course-catalogue')
      },
      {
        path: 'comment',
        name: 'comment',
        component: () => import('../components/ke-course-comment')
      }
    ]
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('../views/Search.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  const isLogin = localStorage.ke_token
  if (isLogin) {
    next()
  } else if (to.path === '/login' || to.path === '/register' || to.path === '/home') {
    next()
  } else {
    next('/login')
  }
})

export default router
